lazy val server = (project in file("server"))
  .settings(commonSettings)
  .settings(
    scalaJSProjects := Seq(client),
    pipelineStages in Assets := Seq(scalaJSPipeline),
    pipelineStages := Seq(digest, gzip),
    // triggers scalaJSPipeline when using compile or continuous compilation
    compile in Compile := ((compile in Compile) dependsOn scalaJSPipeline).value,
    libraryDependencies ++= Seq(
      "com.vmunier" %% "scalajs-scripts" % "1.1.4",
      guice,
      specs2 % Test,
      "com.typesafe.play" %% "play-json" % "2.9.0",
    )
  )
  .enablePlugins(PlayScala)
  .dependsOn(sharedJvm)

lazy val client = (project in file("client"))
  .settings(commonSettings)
  .settings(
    npmDependencies in Compile += "react" -> "16.13.1",
      npmDependencies in Compile += "react-dom" -> "16.13.1",
npmDependencies in Compile += "react-proxy" -> "1.1.8",

npmDevDependencies in Compile += "file-loader" -> "6.0.0",
npmDevDependencies in Compile += "style-loader" -> "1.2.1",
npmDevDependencies in Compile += "css-loader" -> "3.5.3",
npmDevDependencies in Compile += "html-webpack-plugin" -> "4.3.0",
npmDevDependencies in Compile += "copy-webpack-plugin" -> "5.1.1",
npmDevDependencies in Compile += "webpack-merge" -> "4.2.2",

libraryDependencies += "me.shadaj" %%% "slinky-web" % "0.6.5",
libraryDependencies += "me.shadaj" %%% "slinky-hot" % "0.6.5",

libraryDependencies += "org.scalatest" %%% "scalatest" % "3.1.1" % Test,

scalacOptions += "-Ymacro-annotations",

version in webpack := "4.43.0",
version in startWebpackDevServer:= "3.11.0",

webpackResources := baseDirectory.value / "webpack" * "*",

webpackConfigFile in fastOptJS := Some(baseDirectory.value / "webpack" / "webpack-fastopt.config.js"),
webpackConfigFile in fullOptJS := Some(baseDirectory.value / "webpack" / "webpack-opt.config.js"),
webpackConfigFile in Test := Some(baseDirectory.value / "webpack" / "webpack-core.config.js"),

webpackDevServerExtraArgs in fastOptJS := Seq("--inline", "--hot"),
webpackBundlingMode in fastOptJS := BundlingMode.LibraryOnly(),

requireJsDomEnv in Test := true,

addCommandAlias("dev", ";fastOptJS::startWebpackDevServer;~fastOptJS"),

addCommandAlias("build", "fullOptJS::webpack"),
    scalaJSUseMainModuleInitializer := true,
    libraryDependencies ++= Seq(
      "org.scala-js" %%% "scalajs-dom" % "1.1.0",
      "org.querki" %%% "jquery-facade" % "2.0"
    ),
    // Enable macro annotations by setting scalac flags for Scala 2.13
    scalacOptions ++= {
      import Ordering.Implicits._
      if (VersionNumber(scalaVersion.value).numbers >= Seq(2L, 13L)) {
        Seq("-Ymacro-annotations")
      } else {
        Nil
      }
    },
    libraryDependencies ++= {
      import Ordering.Implicits._
      if (VersionNumber(scalaVersion.value).numbers >= Seq(2L, 13L)) {
        Nil
      } else {
        Seq(compilerPlugin("org.scalamacros" % "paradise" % "2.1.1" cross CrossVersion.full))
      }
    },
    libraryDependencies += "org.lrng.binding" %%% "html" % "1.0.3+6-55950506"
  )
  .enablePlugins(ScalaJSPlugin, ScalaJSWeb,ScalaJSBundlerPlugin)
  .dependsOn(sharedJs)

lazy val shared = crossProject(JSPlatform, JVMPlatform)
  .crossType(CrossType.Pure)
  .in(file("shared"))
  .settings(commonSettings)
  .jsConfigure(_.enablePlugins(ScalaJSWeb))
lazy val sharedJvm = shared.jvm
lazy val sharedJs = shared.js

lazy val commonSettings = Seq(
  scalaVersion := "2.13.3",
  organization := "net.osin"
)
